package com.sciencep.pro.common.utils;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @author libe
 */
public class PageHelper {

    private PageHelper() {

    }

    public static <T>Page<T> getPageFromList(List<T> data,int pageNo,int pageSize){
        if(CollUtil.isEmpty(data)){
            return new Page<T>().setCurrent(pageNo).setSize(pageSize);
        }
        int total = data.size();

        int fromIndex = (pageNo - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        if (toIndex > total) {
            toIndex = total;
        }
        if (fromIndex > total) {
            return new Page<T>().setCurrent(pageNo).setSize(pageSize);
        }
        List<T> records = data.subList(fromIndex, toIndex);

        Page<T> page = new Page<>();
        page.setRecords(records);
        page.setCurrent(pageNo).setSize(pageSize).setTotal(total);

        return page;

    }
}
